using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using Fusion.Frameworks.Assets;

namespace Fusion.Frameworks.UI
{
    public class UIUtility
    {
        public static void RegisterButtonAction(GameObject gameObject, Action callback)
        {
            Button button = gameObject.GetComponent<Button>();
            RegisterButtonAction(button, callback);
        }

        public static void RegisterButtonAction(Button button, Action callback)
        {
            button.onClick.RemoveAllListeners();
            button.onClick.AddListener(delegate
            {
                callback();
            });
        }

        public static void RegisterToggleAction(GameObject gameObject, Action<bool> callback)
        {
            Toggle toggle = gameObject.GetComponent<Toggle>();
            RegisterToggleAction(toggle, callback);
        }

        public static void RegisterToggleAction(Toggle toggle, Action<bool> callback)
        {
            toggle.onValueChanged.RemoveAllListeners();
            toggle.onValueChanged.AddListener(delegate (bool isOn)
            {
                callback(isOn);
            });
        }

        public static void SetText(GameObject gameObject, string str)
        {
            TextMeshProUGUI textMeshProUGUI = gameObject.GetComponent<TextMeshProUGUI>();
            SetText(textMeshProUGUI, str);
        }

        public static void SetText(TextMeshProUGUI textMeshProUGUI, string str)
        {
            textMeshProUGUI.text = str;
        }

        public static void SetImage(GameObject gameObject, string str)
        {
            AssetsUtility.SetSpriteOfImage(gameObject, str);
        }

        public static void SetImage(Image image, string str)
        {
            AssetsUtility.SetSprite(image, str);
        }

        public static void RegisterSliderChangedAction(GameObject gameObject, Action<float> callback)
        {
            Slider slider = gameObject.GetComponent<Slider>();
            RegisterSliderChangedAction(slider, callback);
        }

        public static void RegisterSliderChangedAction(Slider slider, Action<float> callback)
        {
            slider.onValueChanged.RemoveAllListeners();
            slider.onValueChanged.AddListener(delegate (float value)
            {
                callback(value);
            });
        }

        public static void RegisterInputFieldChangedAction(GameObject gameObject, Action<string> callback)
        {
            TMP_InputField inputField = gameObject.GetComponent<TMP_InputField>();
            RegisterInputFieldChangedAction(inputField, callback);
        }

        public static void RegisterInputFieldChangedAction(TMP_InputField inputField, Action<string> callback)
        {
            inputField.onValueChanged.RemoveAllListeners();
            inputField.onValueChanged.AddListener(delegate (string value)
            {
                callback(value);
            });
        }

        public static void RegisterInputFieldSubmitAction(GameObject gameObject, Action<string> callback)
        {
            TMP_InputField inputField = gameObject.GetComponent<TMP_InputField>();
            RegisterInputFieldSubmitAction(inputField, callback);
        }

        public static void RegisterInputFieldSubmitAction(TMP_InputField inputField, Action<string> callback)
        {
            inputField.onSubmit.RemoveAllListeners();
            inputField.onSubmit.AddListener(delegate (string value)
            {
                callback(value);
            });
        }

        public static void RegisterInputFieldEndEditAction(GameObject gameObject, Action<string> callback)
        {
            TMP_InputField inputField = gameObject.GetComponent<TMP_InputField>();
            RegisterInputFieldEndEditAction(inputField, callback);
        }

        public static void RegisterInputFieldEndEditAction(TMP_InputField inputField, Action<string> callback)
        {
            inputField.onEndEdit.RemoveAllListeners();
            inputField.onEndEdit.AddListener(delegate (string value)
            {
                callback(value);
            });
        }
    }

}
